import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:tree_hole/letters/SingleDiary.dart';


class AllDiaries extends StatefulWidget {

  String token;
  AllDiaries(this.token);

  @override
  _AllDiaries createState() => new _AllDiaries();
}

class _AllDiaries extends State<AllDiaries> {

  List diaries=[{"title":"还没有题目哦","content":"还没有内容哦"}];
  Response response;
  int code;
  int id;


  void initState(){
    super.initState();
    _getDiary();
  }

  //获取所有日记
  _getDiary(){
    Dio().post("http://121.196.101.7:8080/treehole/diary/allDiary",
        options:Options(headers: {"Token":widget.token},) ).then((value) {
      response=value;
      print(response.data);
      code=response.data["code"];
      if(code==0){
        setState(() {
          this.diaries=response.data["data"];
        });
      }else{
        Fluttertoast.showToast(
          msg: "获取失败，请重试",
          backgroundColor: Colors.amber[50],
          textColor: Colors.black,
          fontSize: 15,
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,);
      }
    });
  }

  //删除提示对话框
  _tips(){
    showDialog(
        context: context,
        builder:(context){
          return AlertDialog(
            title: Text("提示"),
            content: Text("你真的要删除这篇日记吗？"),
            actions: [
              FlatButton(
                child: Text("取消",style: TextStyle(color: Colors.black),),
                color: Colors.greenAccent[100],
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(30)
                ),
                onPressed: (){
                  Navigator.pop(context);
                },),
              FlatButton(
                child: Text("确定",style: TextStyle(color: Colors.black),),
                color: Colors.greenAccent[100],
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(30)
                ),
                onPressed: (){
                  Navigator.pop(context);
                  _deleteDiary();
                },)
            ],
            backgroundColor:Colors.lightGreen[50],
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10)
            ),
          );
        }
    );
  }

  //删除某篇日记
  _deleteDiary(){
    Dio().post("http://121.196.101.7:8080/treehole/diary/delete",
        options:Options(headers: {"Token":widget.token},),queryParameters: {"diaryId":this.id} ).then((value) {
      response=value;
      print(response.data);
      code=response.data["code"];
      if(code==0){
        setState(() {
          _getDiary();
          Fluttertoast.showToast(
            msg: "删除日记成功",
            backgroundColor: Colors.amber[50],
            textColor: Colors.black,
            fontSize: 15,
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.BOTTOM,);
        });
      }else{
        Fluttertoast.showToast(
          msg: "删除失败，请重试",
          backgroundColor: Colors.amber[50],
          textColor: Colors.black,
          fontSize: 15,
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,);
      }
    });
  }


  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: new Text('我的日记本',style:TextStyle(color: Colors.black)),
        backgroundColor: Colors.white70,
        leading: IconButton(
          icon:Icon(Icons.arrow_back,color: Colors.black),
          onPressed: () {Navigator.pop(context);},
        ),
      ),
      body:Container(
        child:  ListView(
          children: this.diaries.map((value){
            return Card(
              margin: EdgeInsets.all(20),
              child: Column(
                children:<Widget> [
                  ListTile(
                    leading: CircleAvatar(
                      backgroundImage: NetworkImage("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3855998524,74332752&fm=26&gp=0.jpg"),
                    ),
                    title: Text(value["title"]),
                    subtitle: Text("今日晴，一直晴❤"),
                    trailing: IconButton(
                      icon: Icon(Icons.delete_rounded,color: Colors.lightGreenAccent[100]),
                      onPressed: (){
                        this.id=value["diaryId"];
                        _tips();
                      },
                    ),
                    onTap: () async {
                      await Navigator.push(context, MaterialPageRoute(builder: (context) {
                        return SingleDiary(widget.token,value["title"],value["content"],value["diaryId"]);
                      }));
                      setState(() {
                        _getDiary();
                      });
                    },
                  ),
                  Container(
                    height: 80,
                    padding: EdgeInsets.all(15),
                    decoration: BoxDecoration(
                      color: Colors.white,
                    ),
                    child: Center(
                      child: Text(value["content"],textAlign: TextAlign.center,
                        overflow:TextOverflow.ellipsis,
                        maxLines: 3,
                        style: TextStyle(color: Colors.black,
                       ),),
                    )
                  ),
                ],
              ),
            );
          }).toList(),
        ),
      ),
      backgroundColor: Colors.amber[50],
    );


  }

}